﻿using B.S.Base.Read.API.Dto;
using MediatR;
using Microsoft.EntityFrameworkCore;
using WEBAPI.API.Application.Command;
using WEBAPI.Damoin;
using WEBAPI.ErroeCode;
using WEBAPI.Indaseture.Infasetures;

namespace B.S.Base.Read.API.Application.CommandHandLer
{
    public class GetRoleCommandHandLer : IRequestHandler<GetRoleCommand, APIResult<APIPaing<List<RoleModel>>>>
    {

        IBaseRepostiy<RoleModel> _roleModel;

        public GetRoleCommandHandLer( IBaseRepostiy<RoleModel> roleModel)
        {
  
            _roleModel = roleModel;
        }

        public async Task<APIResult<APIPaing<List<RoleModel>>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            var list =_roleModel.GetAll().Where(x=>!x.IsDeleted);
            int totalCount = await list.CountAsync();
            int totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            //分页
            list = list.OrderByDescending(x => x.RoleId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            return new APIResult<APIPaing<List<RoleModel>>>
            {
                Code = ResultCode.Ok,
                Data = new APIPaing<List<RoleModel>>
                {
                    Data = list.ToList(),
                    totalCount = totalCount,
                    totalPage = totalPage,
                },
                Message = "获取成功"
            };
        }
    }
}
